package fr.kirin.logger.model.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;

import org.jdesktop.swingx.JXTree;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.SearchPredicate;

import fr.kirin.logger.filter.FilterGUI;
import fr.kirin.logger.i18n.I18N;
import fr.kirin.logger.model.LoggerTreeModel;



/**
 * Tree component for displaying event.
 * This component provide a sort mecanism and a filtering mecanism.
 * the filter are implemented with both business and graphic component.
 * implmeent the MVC pattern.
 * 
 * @author kirin
 *
 */
public class TreeDisplayer {


	public static final int MAX_HEIGHT = 30;
	public static final int MAX_WIDTH = 30;
	private static final int FILTER_SPACING = 5;
	private final LoggerTreeModel model;
	private JPanel main;
	private JXTree tree;
	private JToolBar toolBar;

	
	/**
	 * 
	 *build the tree with a model 
	 * @param model
	 */
	public TreeDisplayer(LoggerTreeModel model){
		this.model = model;

		build();
	}


	/**add a filter to the tree.
	 * 
	 * @param filter
	 */
	public void addFilter(FilterGUI filter){
		filter.registerModel(model);
		toolBar.add(Box.createHorizontalStrut(FILTER_SPACING));
		toolBar.add(filter.getGUI());
	}

	private JToolBar createToolBar(final JXTree tree){
		toolBar = new JToolBar();
		toolBar.add(Box.createHorizontalGlue());
	      
	      
		toolBar.add(new JLabel(I18N.getMessage("search")));
		toolBar.add(Box.createHorizontalStrut(4));
		final SearchPredicate impossiblePredicate = new SearchPredicate(Pattern.compile("\\a"));
		//TODO ajuster les couleurs ici
		//final ColorHighlighter hi = new ColorHighlighter(null,Color.PINK,impossiblePredicate);
		final ColorHighlighter hi = new ColorHighlighter(null,new Color(94,148,250),Color.BLACK,new Color(28,30,130),impossiblePredicate);
		final JTextField searchField = new JTextField();
		searchField.setToolTipText(I18N.getMessage("hightlight"));
        Dimension searcDim = new Dimension(120,MAX_HEIGHT);
        searchField.setPreferredSize(searcDim);
		searchField.setMaximumSize(searcDim);
		searchField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				if(searchField.getText().length() != 0)
					hi.setHighlightPredicate(new SearchPredicate(Pattern.compile("\\Q"+searchField.getText()+"\\E")));
				else
					hi.setHighlightPredicate(impossiblePredicate);
				
				
			}
		
		});
		tree.addHighlighter(hi);
		
		toolBar.add(searchField);
		
		JButton alphaSort = new JButton(IconFactory.loadIcon("/resources/alphaSort.png"));
		alphaSort.setToolTipText("sort");
		Dimension buttonsSize = new Dimension(MAX_WIDTH,MAX_HEIGHT);
		alphaSort.setPreferredSize(buttonsSize);
		alphaSort.setMaximumSize(buttonsSize);
		alphaSort.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    if(tree.getSelectionPath()  == null)
			        model.sort();
			    else
			        model.sort((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()));
			}
		});

		toolBar.add(Box.createHorizontalStrut(FILTER_SPACING));
		toolBar.add(alphaSort);

		return toolBar;
	}


	private void build(){
		main = new JPanel(new BorderLayout());

		tree = new JXTree(model);
		
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		JScrollPane scroll = new JScrollPane(tree);


		main.add(createToolBar(tree),BorderLayout.NORTH);
		main.add(scroll,BorderLayout.CENTER);
	}

	
	

	public void setMaximumSize(Dimension maximumSize)
    {
        main.setMaximumSize(maximumSize);
    }


    public void setMinimumSize(Dimension minimumSize)
    {
        main.setMinimumSize(minimumSize);
    }


    public void setPreferredSize(Dimension preferredSize)
    {
        main.setPreferredSize(preferredSize);
    }


    public void setSize(int width, int height)
    {
        main.setSize(width, height);
    }


    public JComponent getUI(){
		return main;
	}


	public TreeCellRenderer getCellRenderer() {
		return tree.getCellRenderer();
	}


	public void setCellRenderer(TreeCellRenderer x) {
		tree.setCellRenderer(x);
	}


    public void setSelectionModel(TreeSelectionModel selectionModel)
    {
        tree.setSelectionModel(selectionModel);
    }






}
